package com.jynn.mesh.controller;

import com.github.qrpcode.domain.WordGo;
import com.github.qrpcode.domain.WordTable;
import com.jynn.mesh.util.ExcelToHtml;
import com.jynn.mesh.util.FileHandleUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

@RestController
@RequestMapping("/poi")
public class PoiViewController {
	
	@RequestMapping(value = "/index")
	public String index() {
		WordGo wordGo = new WordGo();
		wordGo.add("Hello", "font-size: 20");
//...
		WordTable wordTable = new WordTable(5, 6, "column-width:1=50%; template: normal2; width:50%");
//创建一个表格
		wordTable.merge(2, 4, 4 ,5);
//合并表格
		wordTable.add(2, 3, "aaa", "bbb", "ccc");
//填充数据
		wordGo.add(wordTable);
//添加到word中
//...
		wordGo.create("/C://Users//95117//Desktop/Documents//demo.docx");
		return "index";
	}
	
	/**
	 * 文件在线预览 
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public void view(HttpServletResponse response,@RequestParam(required = false,value="pathFile") String pathFile) {
		
		try {
			if(pathFile==null || new File(pathFile).exists() == false){
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				String message = "<div style='width:100%;text-align:center;color:red;'>文件不存在</div>";
				out.write(message);
				out.close();
				return;
			}
			String[] strArray = pathFile.split("\\.");
			int suffixIndex = strArray.length-1;
			String fileName = pathFile;
			//输出位置&输出名称
			String outPath = "/Users/liuwenxu/Documents/preview/";
			File outfile = new File(outPath);
			if(outfile.exists()==false) {
				outfile.mkdir();
			}
			String htmlName = "1.html";
			String outPutFile = outPath+htmlName;
			//判断文件类型
			if ("doc".equals(strArray[suffixIndex])) {
				FileHandleUtils.docToHtml(outPath, fileName, outPutFile);
			} else if ("docx".equals(strArray[suffixIndex])) {
				FileHandleUtils.docxToHtml(outPath, fileName, outPutFile);
			} else if ("xls".equals(strArray[suffixIndex])) {
				FileHandleUtils.HSSFToHtml(outPath, fileName, outPutFile);
			} else if ("xlsx".equals(strArray[suffixIndex])) {
				ExcelToHtml.excelToHtml(outPath,fileName, outPutFile);
			} else if ("ppt".equals(strArray[suffixIndex])) {
				FileHandleUtils.ppt2Image(fileName, htmlName, outPath);
			} else if ("pptx".equals(strArray[suffixIndex])) {
				FileHandleUtils.pptx2Image(fileName, htmlName, outPath);
			}else if ("vsdx".equals(strArray[suffixIndex])) {
				FileHandleUtils.csdxToImage(fileName, htmlName, outPath);
			}else if ("pdf".equals(strArray[suffixIndex])) {
				FileCopyUtils.copy(new FileInputStream(fileName), response.getOutputStream());
			} else {
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				String message = "<div style='width:100%;text-align:center;color:red;'>抱歉！当前文件格式不支持预览</div>";
				out.write(message);
				out.close();
				return;
			}
			File outFile = new File(outPutFile);
			if(outFile.exists()!=false) {
				//将生成的html文件返回给前端页面
				FileCopyUtils.copy(new FileInputStream(outPutFile), response.getOutputStream());
			}
//			delAllFile(outPath);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	//删除文件夹  
	private static void delFolder(String folderPath) {  
	     try {  
	        delAllFile(folderPath); //删除完里面所有内容  
	        String filePath = folderPath;  
	        filePath = filePath.toString();  
	        java.io.File myFilePath = new java.io.File(filePath);  
	        myFilePath.delete(); //删除空文件夹  
	     } catch (Exception e) {  
	       e.printStackTrace();   
	     }
	}  
	
	//删除指定文件夹下的所有文件
	private static boolean delAllFile(String path) {  
	       boolean flag = false;
	       File file = new File(path);  
	       if (!file.exists()) {  
	         return flag;  
	       }
	       if (!file.isDirectory()) {  
	         return flag;
	       }
	       String[] tempList = file.list();  
	       File temp = null;  
	       for (int i = 0; i < tempList.length; i++) {  
	          if (path.endsWith(File.separator)) {  
	             temp = new File(path + tempList[i]);  
	          } else {  
	              temp = new File(path + File.separator + tempList[i]);  
	          }  
	          if (temp.isFile()) {
	             temp.delete();  
	          }  
	          if (temp.isDirectory()) {  
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;  
	          }  
	       }  
	       return flag;  
	     }  
	}  
